package com.auth.web.spring;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.auth.domain.T_branch;
import com.auth.domain.T_function;

public class AuthUtils {

	private static Logger log = Logger.getLogger(AuthUtils.class.getName());

	/**
	 * 对function进行sort排序，按照showseq，分级排序；显示成一棵树，
	 * 前提条件是，inlist的数据要首先sql中执行： order by functlevel,  showseq;然后再用此方法就可以排列成一棵树
	 * @param inlist
	 * @return
	 */
	public static List sortFunctionList(List inlist){
		//Collections.sort(inlist);
		T_function one, two, three, four, five;

		List mubiaoList = new ArrayList();
		List oneLevel = new ArrayList();
		List twoLevel = new ArrayList();
		List threeLevelList = new ArrayList();
		List fourLevelList = new ArrayList();
		List fiveLevelList = new ArrayList();

		Iterator it, it2, it3, it4, it5;

		it = inlist.iterator();
		while(it.hasNext()){
			one = (T_function)it.next();
			if(one.getFunctlevel().equals("0")||one.getParentid()==null){
				mubiaoList.add(one);
			}else if(one.getFunctlevel().equals("1")){
				mubiaoList.add(one);
				log.debug("------1Leveladd="+one.getFunctname()+";functid="+one.getFunctid());

				it2 = inlist.iterator();
				while(it2.hasNext()){
					two = (T_function)it2.next();
					//注意：one.getFunctid()为Integer类型，是对象，判断是否相等要用equals方法，用==会出现false的情况；
					//log.debug("------------2Level "+two.getFunctname()+";"+one.getFunctid()+";"+two.getParentid()+"?是否相等="+(two.getParentid()==one.getFunctid()));
					//log.debug("------------2Level，当前元素是否需要计入:two.getParentid()="+two.getParentid()+";one.getFunctid()="+one.getFunctid()+";是否相等="+(two.getParentid()==one.getFunctid()));

					if(two.getParentid()!=null&&two.getParentid().equals(one.getFunctid())){
						mubiaoList.add(two);
						log.debug("------------2Level.add="+two.getFunctname()+";functid="+two.getFunctid());

						it3 = inlist.iterator();
						while(it3.hasNext()){
							three = (T_function)it3.next();
							if(three.getParentid()!=null&&three.getParentid().equals(two.getFunctid())){
								mubiaoList.add(three);
								log.debug("------------------3Level.add="+three.getFunctname()+";functid="+three.getFunctid());

								it4 = inlist.iterator();
								while(it4.hasNext()){
									four = (T_function)it4.next();
									if(four.getParentid()!=null&&four.getParentid().equals(three.getFunctid())){
										mubiaoList.add(four);
										log.debug("------------------4Level.add="+four.getFunctname()+";functid="+four.getFunctid());

										it5 = inlist.iterator();
										while(it5.hasNext()){
											five = (T_function)it5.next();
											if(five.getParentid()!=null&&five.getParentid().equals(four.getFunctid())){
												mubiaoList.add(five);
												log.debug("------------------------5Level.add="+five.getFunctname()+";functid="+five.getFunctid());
											}
										}
									}
								}
							}
						}
					}
				}
				log.debug("------1Level循环结束------------------"+one.getFunctname());
			}
		}
		return mubiaoList;
	}

	/**
	 * 对branch进行sort排序，按照showseq，分级排序；显示成一棵树，
	 * 前提条件是，inlist的数据要首先sql中执行： order by functlevel,  showseq(ext1);然后再用此方法就可以排列成一棵树
	 * @param inlist
	 * @return
	 */
	public static List sortBranchList(List inlist){
		//Collections.sort(inlist);
		T_branch one, two, three, four, five;

		List mubiaoList = new ArrayList();
		List oneLevel = new ArrayList();
		List twoLevel = new ArrayList();
		List threeLevelList = new ArrayList();
		List fourLevelList = new ArrayList();
		List fiveLevelList = new ArrayList();

		Iterator it, it2, it3, it4, it5;

		it = inlist.iterator();
		while(it.hasNext()){
			one = (T_branch)it.next();
			if(one.getBranchlevel().equals("0")||one.getParentbranchid()==null){
				mubiaoList.add(one);
			}else if(one.getBranchlevel().equals("1")){
				mubiaoList.add(one);
				log.debug("------1Leveladd="+one.getName()+";id="+one.getId()+"");

				it2 = inlist.iterator();
				while(it2.hasNext()){
					two = (T_branch)it2.next();
					//注意：one.getFunctid()为Integer类型，是对象，判断是否相等要用equals方法，用==会出现false的情况；
					log.debug("------------2Level "+two.getName()+";"+one.getId()+";"+two.getParentbranchid()+"?是否相等="+(two.getParentbranchid()==(one.getId()+"") ));
					log.debug("------------2Level，当前元素是否需要计入:two.getParentbranchid()="+two.getParentbranchid()+";one.getId()="+one.getId()+";是否相等="+(two.getParentbranchid()==(one.getId()+"")));

					if(two.getParentbranchid()!=null&&two.getParentbranchid().equals(one.getId()+"")){
						mubiaoList.add(two);
						log.debug("------------2Level.add="+two.getName()+";branch id="+two.getId());

						it3 = inlist.iterator();
						while(it3.hasNext()){
							three = (T_branch)it3.next();
							if(three.getParentbranchid()!=null&&three.getParentbranchid().equals(two.getId()+"")){
								mubiaoList.add(three);
								log.debug("------------------3Level.add="+three.getName()+";branch id="+three.getId());

								it4 = inlist.iterator();
								while(it4.hasNext()){
									four = (T_branch)it4.next();
									if(four.getParentbranchid()!=null&&four.getParentbranchid().equals(three.getId()+"")){
										mubiaoList.add(four);
										log.debug("------------------4Level.add="+four.getName()+";branch id="+four.getId());

										it5 = inlist.iterator();
										while(it5.hasNext()){
											five = (T_branch)it5.next();
											if(five.getParentbranchid()!=null&&five.getParentbranchid().equals(four.getId()+"")){
												mubiaoList.add(five);
												log.debug("------------------------5Level.add="+five.getName()+";branch id="+five.getId());
											}
										}
									}
								}
							}
						}
					}
				}
				log.debug("------1Level循环结束------------------"+one.getName());
			}
		}
		return mubiaoList;
	}

}
